#ifndef BACKEND_H
#define BACKEND_H

#include <QObject>
#include <QString>

#include "QDebug"

//Integrating QML and C++
//The most common and quickest way to do this is to expose the C++ class to the QML runtime, provided the C++ implementation is derived from QObject.


class BackEnd : public QObject
{
    Q_OBJECT
    //The Q_PROPERTY macro declares a property that could be accessed from QML.
    Q_PROPERTY(QString userName READ userName WRITE setUserName NOTIFY userNameChanged)
    //ageAlias是在QML使用的别名,m_userAge是对应的变量名称
    //CONSTANT说明是只读的
    Q_PROPERTY(QString ageAlias MEMBER m_userAge CONSTANT)
    //将枚举类型注册，QML即可访问
    Q_ENUMS(MyColor)
public:
    explicit BackEnd(QObject *parent = nullptr);

    enum MyColor
    {
        None = 0,
        MyRed = 1,
        MyBlack
    };

    QString userName();
    Q_INVOKABLE void myTestInvokableFunc(){qDebug() << "do myTestInvokableFunc";} //函数声明使用 Q_INVOKABLE 后，可在qml文件中调用
    Q_INVOKABLE void testEnum(MyColor color){
        qDebug() << "get color" << color;
    }
    void setUserName(const QString &userName);

public slots:
    void myTestPublicSlotsFunc(){qDebug() << "do myTestPublicSlotsFunc";} // public slots可直接被qml调用

signals:
    void userNameChanged();
    void signalTest(int colorParam);

private:
    QString m_userName;
    QString m_userAge;
};

#endif // BACKEND_H
